package com.wormhole.wormholemanagement.controller;

import com.wormhole.wormholemanagement.dto.PersonPositionDTO;
import com.wormhole.wormholemanagement.dto.PersonPositionResponseDTO;
import com.wormhole.wormholemanagement.service.PersonPositionService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "姓名职务管理")
@RequestMapping("/api/person-positions")
@RequiredArgsConstructor
public class PersonPositionController {
    private final PersonPositionService personPositionService;

    @GetMapping("/search")
    public ResponseEntity<List<PersonPositionResponseDTO>> searchPersonPositions(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String title) {
        List<PersonPositionResponseDTO> result = personPositionService.searchPersonPositions(name, title);
        return ResponseEntity.ok(result);
    }

    @PostMapping
    public ResponseEntity<PersonPositionResponseDTO> addPersonPosition(@RequestBody PersonPositionDTO dto) {
        PersonPositionResponseDTO result = personPositionService.addPersonPosition(dto);
        return ResponseEntity.status(HttpStatus.CREATED).body(result);
    }

    @PutMapping("/{id}")
    public ResponseEntity<PersonPositionResponseDTO> updatePersonPosition(
            @PathVariable Long id,
            @RequestBody PersonPositionDTO dto) {
        PersonPositionResponseDTO result = personPositionService.updatePersonPosition(id, dto);
        return ResponseEntity.ok(result);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletePersonPosition(@PathVariable Long id) {
        personPositionService.deletePersonPosition(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping
    public ResponseEntity<List<PersonPositionResponseDTO>> getAllPersonPositions() {
        List<PersonPositionResponseDTO> result = personPositionService.getAllPersonPositions();
        return ResponseEntity.ok(result);
    }
}